<template>
  <footer>
    <div class="dfooter">
      <van-goods-action>
        <van-goods-action-icon icon="chat-o" @click="gotoComment(goodDetail)" />
        <van-goods-action-icon
          icon="cart-o"
          :badge="cartsCount"
          @click="gotowhere('cart')"
        />
        <van-goods-action-button
          color="#be99ff"
          type="warning"
          text="加入购物车"
          @click="addToCart"
        />
        <van-goods-action-button
          color="#ff80aa"
          type="danger"
          text="立即购买"
        />
      </van-goods-action>
      <!-- <router-link :to="{ name: l.name }">
      </router-link> -->
    </div>
  </footer>
</template>

<script>
export default {
  data() {
    return { show: false };
  },
  props: ["goodDetail", "cartsCount"],
  methods: {
    // 跳转到评论组件，携带商品详情数据
    gotoComment(goodDetail) {
      this.$router.push({ name: "comment", params: goodDetail });
    },
    async addToCart() {
      // 如果有用户信息则进行后续操作，如果没有跳转去登录
      // 判断cart库里是否有相同itemsId的商品，无则直接添加，有则数量++
      if (this.userInfo) {
        let r1 = await this.$ajax.ifAlreadyHas({
          phone: this.userInfo.phone,
          itemsId: this.goodDetail.itemsId,
        });
        if (r1.length > 0) {
          let r2 = await this.$ajax.addCount1(r1[0].id, {
            count: r1[0].count + 1,
          });
          if (r2) {
            this.ShowSuccess("添加商品数量成功");
            // console.log(r1);
          }
        } else {
          let r3 = await this.$ajax.addDataToCart({
            phone: this.userInfo.phone,
            itemsId: this.goodDetail.itemsId,
            img: this.goodDetail.img[0],
            name: this.goodDetail.name,
            count: 1,
            price: this.goodDetail.price,
          });
          if (r3) {
            this.ShowSuccess("添加成功");
          }
          // console.log(res);
        }
      } else {
        this.$dialog
          .alert({
            title: "您还未登录，请先登录",
            message: "登录后了解更多~",
            theme: "round-button",
          })
          .then(() => {
            //on confirm
            this.$router.push({ name: "cart" });
          });
      }
      this.$parent.getCart();
    },
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
.dfooter {
  ::v-deep .van-goods-action {
    border-top: 1px solid #ccc;
    padding: 4px 10px;
  }
}
</style>